package com.ys.service.sys.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.ys.entity.sys.SysCol;
import com.ys.entity.sys.SysMenu;
import com.ys.mapper.sys.SysColMapper;
import com.ys.service.sys.SysColService;
import com.ys.vo.R;
import com.ys.vo.W;

@Service("sysColService")
public class SysColServiceImpl implements SysColService {
	
	public static String FORM_BASE = 
			"		<div class=\"layui-form-item\">\n" + 
			"			<div class=\"layui-col-md5\">\n" + 
			"				<label class=\"layui-form-label\">RL_FOR_TITLE</label>\n" + 
			"				<div class=\"layui-input-block\">\n" + 
			"					<input type=\"text\" name=\"RL_FOR_CODE\" placeholder=\"请输入RL_FOR_TITLE\" class=\"layui-input\" lay-verify=\"required\">\n" + 
			"				</div>\n" + 
			"			</div>\n" + 
			"		</div>\n";

	@Autowired
	private SysColMapper sysColMapper;

	@Override
	public R add(Long menuId, String name, String code) {

		if (menuId == null) {
			return R.error("菜单ID不能为空");
		} else if (new SysMenu().setId(menuId).count() == 0) {
			return R.error("菜单不存在");
		}
		SysMenu sysMenu = new SysMenu().setId(menuId).queryFirst();
		if (sysMenu == null) {
			return R.error("菜单不存在");
		}
		
		if (name == null || name.length() == 0) {
			return R.error("字段名称不能为空");
		}

		if (code == null || code.length() == 0) {
			return R.error("字段编码不能为空");
		}
		
		// 验证是否已存在
		if (new SysCol().setCode(code).setMenuId(menuId).count() > 0) {
			return R.error("该字段已存在");
		}
		
		Integer sortNo = 1;
		Integer authType = 1;
		String frontType = "t_text";
		Integer frontEdit = 0;
		Integer frontShow = 0;
		Integer frontWidth = 80;
		Integer frontFixed = 0;
		Integer frontAlign = 0;
		Integer frontConditionState = 0;
		String frontConditionType = "t_text";
		String frontConditionRangeType = "t_text";
		String frontConditionCode = null;
		String frontConditionDefault = null;
		String dictKeyBase = null;
		Integer formLength = 80;
		Integer formAddState = 0;
		String formAddType = "t_text";
		Integer formAddRequired = 0;
		Integer formAddReadonly = 0;
		Integer formAddHidden = 0;
		Integer formEditState = 0;
		String formEditType = "t_text";
		Integer formEditRequired = 0;
		Integer formEditReadonly = 0;
		Integer formEditHidden = 0;
		Integer importState = 0;
		Integer importSortNo = 0;
		String modalKey = null;
		String modalRelativeCode = null;
		String modalValueCode = null;
		String dropdownRequestPath = null;
		Integer multyFileSourceType = null;
		
		SysCol sysCol = SysCol.sysCol(menuId, name, code, sortNo, authType,
				frontType, frontEdit, frontShow, frontWidth, frontFixed, frontAlign, frontConditionState, frontConditionType, frontConditionRangeType, frontConditionCode, frontConditionDefault, dictKeyBase,
				formLength, formAddState, formAddType, formAddRequired, formAddReadonly, formAddHidden, formEditState, formEditType, formEditHidden, formEditRequired, formEditReadonly,
				importState, importSortNo, modalKey, modalRelativeCode, modalValueCode, dropdownRequestPath, multyFileSourceType, 1);
		sysColMapper.insert(sysCol);
		
		return R.ok("操作成功");
	}

	@Override
	@Transactional
	public R deleteTrue(Long id) {
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		// 删除字段
		sysColMapper.deleteTrue(id);
		
		return R.ok();
	}


	@Override
	public String toAdd(Model model, Long menuId) {
		if (menuId == null) {
			return W.error(model, "菜单ID不能为空");
		}
		SysMenu sysMenu = new SysMenu().setId(menuId).queryFirst();
		
		ToAddSysColDto dto = new ToAddSysColDto(sysMenu.getId(), sysMenu.getName());
		
		model.addAttribute("dto", dto);
		return "sys/sysCol/toAdd";
	}
}
